package com.lg.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import java.lang.Math;
import com.ibatis.sqlmap.client.SqlMapClient;

public class CTurnPage  {

	private String formName;
	private String tableName;
	private static final int firstPage = 1;//第一页
	private int curPage = 1;//当前页
	private int totalCount;//总记录数
	private int totalPage;//总页数
	private List sqlList = null;
	private String sqlStr;//查询语句
	private int perPageRows = 10;//每页显示的记录数
	private int perStartIndex = 0;//每页开始位置
	private Object obj = null;//传过来的参数对象
	private List totalList;//查询所有符合条件的列表信息
	private int startLinkPage = 1;//链接的第一页
	private int lastLinkPage;//链接的最后一页
	private int linkCount=10;//点击链接显示的链接数

	public int getLinkCount() {
		return linkCount;
	}

	public void setLinkCount(int linkCount) {
		this.linkCount = linkCount;
	}

	public int getLastLinkPage() {
		return lastLinkPage;
	}

	public void setLastLinkPage(int lastLinkPage) {
		this.lastLinkPage = lastLinkPage;
	}

	public int getStartLinkPage() {
		return startLinkPage;
	}

	public void setStartLinkPage(int startLinkPage) {
		this.startLinkPage = startLinkPage;
	}

	/**
	 * @return obj
	 */
	public Object getObj() {
		return obj;
	}

	/**
	 * @param obj 要设置的 obj
	 */
	public void setObj(Object obj) {
		this.obj = obj;
	}

	/**
	 * 获取查询结果
	 * @param sqlStr 查询语句 如:TUser.selectById
	 * @param obj    传过来的参数对象
	 * @param perStartIndex  每页开始位置
	 * @param perPageRows    每页总数
	 */
	public void getQueryList(String sqlStr,Object obj,int perStartIndex,int perPageRows){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			this.sqlList = sqlmap.queryForList(sqlStr,obj,perStartIndex,perPageRows);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	public void setQueryList(String sqlStr,Object obj,int perStartIndex,int perPageRows){
		this.setSqlStr(sqlStr);
		this.setObj(obj);
		this.setPerStartIndex(perStartIndex);
		this.setPerPageRows(perPageRows);

	}
	/**
	 * 
	 * @param sqlStr 查询语句 如:TUser.selectById
	 * @param obj 传过来的参数对象
	 * @param perPageRows 每页总数
	 */
	public void getQueryList(String sqlStr,Object obj,int perPageRows){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			this.sqlList = sqlmap.queryForList(sqlStr,obj,perStartIndex,perPageRows);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	public void setQueryList(String sqlStr,Object obj,int perPageRows){
		this.setSqlStr(sqlStr);
		this.setObj(obj);
		this.setPerPageRows(perPageRows);

	}
	public void excute(HttpServletRequest request){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String tempPageNum=request.getParameter("selectNum");
		if(tempPageNum==null || tempPageNum.length()<1){
			curPage = firstPage;
		}else{
			curPage = Integer.parseInt(tempPageNum);
			perStartIndex = (curPage-1)*perPageRows;
		}
		try{
			this.totalList = sqlmap.queryForList(sqlStr,obj);
			this.sqlList = sqlmap.queryForList(sqlStr,obj,perStartIndex,perPageRows);

			totalCount = this.totalList.size();
			if(perPageRows!=0){
				double tempNum = (totalCount+0.0)/perPageRows;
				totalPage = (int)Math.ceil(tempNum);
			}else{
				totalPage = totalCount/5;
			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}


	/**
	 * 获取翻页信息
	 * @return
	 */
	public String getPageInfo(){
		StringBuffer sb = new StringBuffer();
		sb.append("当前为第").append(curPage).append("页,").append("共").append(totalPage).append("页").append(totalCount).append("条记录");
		return sb.toString();
	}


	public String getTurnPageLink(){
		StringBuffer sb = new StringBuffer();
		startLinkPage = curPage - (linkCount/2);

		int temp = totalPage-linkCount+1;
		if(startLinkPage>temp){
			startLinkPage = temp;
		}
		if(startLinkPage<1){
			startLinkPage=1;
		}

		lastLinkPage =  startLinkPage + linkCount;
		if(lastLinkPage>totalPage){
			lastLinkPage = totalPage+1;
		}
		
		//处理向前点击按钮
		if(curPage==1){
			sb.append("    <img  src='/images/pagebutton/btnstart2.gif'/>&nbsp;<img  src='/images/pagebutton/btnpre2.gif'/>&nbsp;");
		}else{
			sb.append("    <img onclick='javascript:selectPage(1)'  style='cursor:hand' src='/images/pagebutton/btnstart1.gif'/>&nbsp;<img onclick='javascript:selectPage(").append(curPage-1).append(")' style='cursor:hand' src='/images/pagebutton/btnpre1.gif'/>&nbsp;");
		}


		for(int i=startLinkPage;i<lastLinkPage;i++){
			if(curPage==i){
			sb.append("<a style='text-decoration:none;font-weight:bold;color:red;font-size:14px' href='javascript:selectPage(").append(i).append(")'>").append(i).append("</a>&nbsp;");
			}else{
				sb.append("<a style='text-decoration:none;font-weight:bold;font-size:14px' href='javascript:selectPage(").append(i).append(")'>").append(i).append("</a>&nbsp;");
			}
		}

       //处理向后点击按钮
		if(curPage==totalPage || totalPage==0){
			sb.append("    <img  src='/images/pagebutton/btnnext2.gif'/>&nbsp;<img src='/images/pagebutton/btnend2.gif'/>&nbsp;");
		}else{
			sb.append("    <img onclick='javascript:selectPage(").append(curPage+1).append(")' style='cursor:hand' src='/images/pagebutton/btnnext1.gif'/>&nbsp;<img onclick='javascript:selectPage(").append(totalPage).append(")' style='cursor:hand' src='/images/pagebutton/btnend1.gif'/>&nbsp;");
		}
		sb.append("<input type='hidden' name='selectNum' value='1'/>");

		return sb.toString();
	}
	/**
	 * 
	 * @param sqlStr 查询语句 如:TUser.selectById
	 * @param perPageRows 每页总数
	 */
	public void getQueryList(String sqlStr,int perPageRows){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			this.sqlList = sqlmap.queryForList(sqlStr,null,perStartIndex,perPageRows);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	public void setQueryList(String sqlStr,Object obj){
		this.setSqlStr(sqlStr);
		this.setObj(obj);

	}

	/**
	 * 查询语句 如:TUser.selectById
	 * @param sqlStr
	 */
	public void getQueryList(String sqlStr){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			this.sqlList = sqlmap.queryForList(sqlStr,null,perStartIndex,perPageRows);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	public void setQueryList(String sqlStr){
		this.setSqlStr(sqlStr);

	}


	public int getCurPage() {
		return curPage;
	}



	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}



	public int getTotalCount() {
		return totalCount;
	}



	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}



	public int getTotalPage() {
		return totalPage;
	}



	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}





	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public int getPerPageRows() {
		return perPageRows;
	}

	public void setPerPageRows(int perPageRows) {
		this.perPageRows = perPageRows;
	}

	public int getPerStartIndex() {
		return perStartIndex;
	}

	public void setPerStartIndex(int perStartIndex) {
		this.perStartIndex = perStartIndex;
	}

	public List getSqlList() {
		return sqlList;
	}

	public void setSqlList(List sqlList) {
		this.sqlList = sqlList;
	}

	public String getSqlStr() {
		return sqlStr;
	}

	public void setSqlStr(String sqlStr) {
		this.sqlStr = sqlStr;
	}

	public List getTotalList() {
		return totalList;
	}

	public void setTotalList(List totalList) {
		this.totalList = totalList;
	}


}
